home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
DELPHI32
/
MATH
/
BIN32
/
BIN32.ZIP
/
BIN32.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-03-25
|
16KB
|
459 lines
{ Demoprogram that uses unit Binary32.
Binary32.dcu is compiled with Delphi« 2.0 (32-bit).
If you do not have this compiler, contact me and I'll send you a 16-bit version
via e-mail (mail attachment).
Integer (base 10), Binary (base 2), Octal (base 8), Hexadecimal (base 16).
Implemented functions:
----------------------
Integer -> Binary and vice versa;
Integer -> Octal and vice versa;
Integer -> Hexadecimal and vice versa;
Binary -> Octal and vice versa;
Binary -> Hexadecimal and vice versa;
Octal -> Hexadecimal and vice versa;
Binary +, -, *, /;
Binary logic: OR, XOR, AND, NOT;
Binary 1 bit/byte left/right shift.
Copyright ⌐1996, John Merrit
Contact: john.merrit@net4all.be
Release History: Version 1.32, released March 25 (finally!) 1996.
This code is provided as is for your use. It may
be modified and redistributed.
This code is not to be sold and must be distributed
for free.
Unit Binary32.dcu may be used freely (you don't need to register your copy) and may
not be sold or distributed for commercial purposes.
( Just use it the way you use a unit included with Delphi«. )
Possible modifications:
------------------------
Addition of:
Binary logic for decimal numbers (very easy when you have the code for Binary32!);
Current largest hexadecimal value = FFFFFFF (= 268435455 decimal = 1777777777 octal = 11...11 binary);
User definable wordsize;
...
100011001111001101100011000000001101100000000001101110111001111111001111110011111001000000000011110000
Function definitions in unit Binary32:
----------------------------------------
General notes:
1. Integer must be a positive ( > 0) integer ( NO fractional part)!
2. #FFFFFFFh = #1777777777o = #111...111b = #268435455d is (currently) the greatest
number supported.
3. User does not need to define a wordsize.
Get the source code for Binary32.dcu (see BinLog32.txt) and implement this
yourself (you already got 24 (if you count them, it's actually 48) functions from me for free!).
type NumberBase = (Hex, Oct, Bin, Dec); // Note that Dec is not used in Binary32.
// Only implemented for future updates.
function IntToBin(LI: longint): string; // Integer to Binary
function BinToInt(LI: string): longint; // Binary to Integer
function IntToOct(LI: longint): string; // Integer to Octal
function MyIntToHex(LI: longint): string; // Integer to Hexadecimal (variant on Borland's IntToHex)
function OctToInt(LI: string): longint; // Octal to Integer
function HexToInt(LI: string): longint; // Hexadecimal to Integer
function BinToOct(LI: string): string; // Binary to Octal
function OctToBin(LI: string): string; // Octal to Binary
function BinToHex(LI: string): string; // Binary to Hexadecimal
function HexToBin(LI: string): string; // Hexadecimal to Binary
function OctToHex(LI: string): string; // Octal to Hexadecimal
function HexToOct(LI: string): string; // Hexadecimal to Octal
// The following functions can only be applied on numbers of base Bin, Oct and Hex.
function BinAdd(B1,B2: string; Base: NumberBase): string; // Binary addition (B1 + B2)
function BinSubtract(B1,B2: string; Base: NumberBase): string; // Binary subtraction (B1 - B2)
function BinMultiply(B1,B2: string; Base: NumberBase): string; // Binary Multiplication (B1 * B2)
function BinDivide(B1,B2: string; Base: NumberBase): string; // Binary division (B1 div B2)
function BinOR(B1,B2: string; Base: NumberBase): string; // Binary B1 OR B2
function BinAND(B1,B2: string; Base: NumberBase): string; // Binary B1 AND B2
function BinXOR(B1,B2: string; Base: NumberBase): string; // Binary B1 XOR B2
function BinNOT(B1: string; Base: NumberBase): string; // Binary NOT B1
function BinShiftLeft(B1: string; Base: NumberBase): string; // 1 Bit left shift on a binary integer
function BinShiftRight(B1: string; Base: NumberBase): string; // 1 Bit right shift on a binary integer
function BinShiftLeftByte(B1: string; Base: NumberBase): string; // 1 Byte left shift on a binary integer
function BinShiftRightByte(B1: string; Base: NumberBase): string; // 1 Byte right shift on a binary integer
}
unit Bin32;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, StdCtrls, Binary32;
type
TForm1 = class(TForm)
Memo1: TMemo;
MainMenu1: TMainMenu;
Edit1: TMenuItem;
Start1: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
Font1: TMenuItem;
FontDialog1: TFontDialog;
Clear1: TMenuItem;
procedure Exit1Click(Sender: TObject);
procedure Start1Click(Sender: TObject);
procedure Font1Click(Sender: TObject);
procedure Clear1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;
{ Main program.
Note that no errortrapping is done!
Not all the implemented functions are showcased below.
Look at the definitions above for proper usage.
The program has been written so that no error will occur.
If you change anything and an error does occur, it's your responsibility, not mine!}
procedure TForm1.Start1Click(Sender: TObject);
var h,i,j: longint;
k,l,m: integer;
s1,s2: string;
Base: NumberBase;
begin
randomize;
for m:= 1 to 5 do
begin { Convert random generated integers to bin, oct and hex... }
i:= random(50000) + 1;
Memo1.Lines.Add(IntToStr(i) + ' to binary: ' + IntToBin(i));
Memo1.Lines.Add(IntToStr(i) + ' to octal: ' + IntToOct(i));
Memo1.Lines.Add(IntToStr(i) + ' to hexadecimal: ' + MyIntToHex(i));
end;
for m:= 1 to 5 do
begin
h:= random(10000) +1; { don't make it 0 }
j:= random(1000) +1; { don't make it 0 }
k:= random(3) +1; { don't make it 0 }
l:= random(4)+1; { don't make it 0 }
case k of
1:begin { Do some arithmetic on random generated integers. }
Base:= Bin; { Let the program decide wich base it will use... }
s1:= IntToBin(h);
s2:= IntToBin(j);
Memo1.Lines.Add('Base = Bin');
Memo1.Lines.Add('h= ' + s1 + ' j = ' + s2);
case l of
1: Memo1.Lines.Add('h + j in Bin = ' + BinAdd(s1,s2,Base));
2: Memo1.Lines.Add('h - j in Bin = ' + BinSubtract(s1,s2,Base));
3: Memo1.Lines.Add('h * j in Bin = ' + BinMultiply(s1,s2,Base));
4: Memo1.Lines.Add('h / j in Bin = ' + BinDivide(s1,s2,Base));
end;
end;
2:begin
Base:= Oct;
s1:= IntToOct(h);
s2:= IntToOct(j);
Memo1.Lines.Add('Base = Oct');
Memo1.Lines.Add('h= ' + s1 + ' j = ' + s2);
case l of
1: Memo1.Lines.Add('h + j in Oct = ' + BinAdd(s1,s2,Base));
2: Memo1.Lines.Add('h - j in Oct = ' + BinSubtract(s1,s2,Base));
3: Memo1.Lines.Add('h * j in Oct = ' + BinMultiply(s1,s2,Base));
4: Memo1.Lines.Add('h / j in Oct = ' + BinDivide(s1,s2,Base));
end;
end;
3:begin
Base:= Hex;
s1:= MyIntToHex(h);
s2:= MyIntToHex(j);
Memo1.Lines.Add('Base = Hex');
Memo1.Lines.Add('h= ' + s1 + ' j = ' + s2);
case l of
1: Memo1.Lines.Add('h + j in Hex = ' + BinAdd(s1,s2,Base));
2: Memo1.Lines.Add('h - j in Hex = ' + BinSubtract(s1,s2,Base));
3: Memo1.Lines.Add('h * j in Hex = ' + BinMultiply(s1,s2,Base));
4: Memo1.Lines.Add('h / j in Hex = ' + BinDivide(s1,s2,Base));
end;
end;
end;
end;
for m:= 1 to 5 do { The following code is added in a hurry. }
begin { It isn't as structured as above. }
i:= random(50000) + 1; { It should still be very readable, though... }
j:= random(50000) + 1; { The program will calculate the OR, XOR and AND on random numbers... }
s1:= IntToOct(i); { ...in each base. }
s2:= IntToOct(j);
Memo1.Lines.Add('Base is octal: ');
Memo1.Lines.Add('i= ' + s1);
Memo1.Lines.Add('h= ' + s2);
s1:= BinOR(s1,s2,Oct);
Memo1.Lines.Add(' i OR h: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
j:= random(50000) + 1;
s1:= IntToOct(i);
s2:= IntToOct(j);
Memo1.Lines.Add('Base is octal: ');
Memo1.Lines.Add('i= ' + s1);
Memo1.Lines.Add('h= ' + s2);
s1:= BinXOR(s1,s2,Oct);
Memo1.Lines.Add('i XOR h: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
j:= random(50000) + 1;
s1:= IntToOct(i);
s2:= IntToOct(j);
Memo1.Lines.Add('Base is octal: ');
Memo1.Lines.Add('i= ' + s1);
Memo1.Lines.Add('h= ' + s2);
s1:= BinAND(s1,s2,Oct);
Memo1.Lines.Add('i AND h: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
j:= random(50000) + 1;
s1:= MyIntToHex(i);
s2:= MyIntToHex(j);
Memo1.Lines.Add('Base is hexadecimal: ');
Memo1.Lines.Add('i= ' + s1);
Memo1.Lines.Add('h= ' + s2);
s1:= BinOR(s1,s2,Hex);
Memo1.Lines.Add('i OR h: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
j:= random(50000) + 1;
s1:= MyIntToHex(i);
s2:= MyIntToHex(j);
Memo1.Lines.Add('Base is hexadecimal: ');
Memo1.Lines.Add('i= ' + s1);
Memo1.Lines.Add('h= ' + s2);
s1:= BinXOR(s1,s2,Hex);
Memo1.Lines.Add('i XOR h: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
j:= random(50000) + 1;
s1:= MyIntToHex(i);
s2:= MyIntToHex(j);
Memo1.Lines.Add('Base is hexadecimal: ');
Memo1.Lines.Add('i= ' + s1);
Memo1.Lines.Add('h= ' + s2);
s1:= BinAND(s1,s2,Hex);
Memo1.Lines.Add('i AND h: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
j:= random(50000) + 1;
s1:= IntToBin(i);
s2:= IntToBin(j);
Memo1.Lines.Add('Base is binary: ');
Memo1.Lines.Add('i= ' + s1);
Memo1.Lines.Add('h= ' + s2);
s1:= BinOR(s1,s2,Bin);
Memo1.Lines.Add('i OR h: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
j:= random(50000) + 1;
s1:= IntToBin(i);
s2:= IntToBin(j);
Memo1.Lines.Add('Base is binary: ');
Memo1.Lines.Add('i= ' + s1);
Memo1.Lines.Add('h= ' + s2);
s1:= BinXOR(s1,s2,Bin);
Memo1.Lines.Add('i XOR h: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
j:= random(50000) + 1;
s1:= IntToBin(i);
s2:= IntToBin(j);
Memo1.Lines.Add('Base is binary: ');
Memo1.Lines.Add('i= ' + s1);
Memo1.Lines.Add('h= ' + s2);
s1:= BinAND(s1,s2,Bin);
Memo1.Lines.Add('i AND h: ' + s1);
end;
{ NOT was last added, so... }
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
s1:= IntToBin(i);
Memo1.Lines.Add('Base is binary: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinNOT(s1,Bin);
Memo1.Lines.Add('NOT i: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
s1:= IntToOct(i);
Memo1.Lines.Add('Base is octal: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinNOT(s1,Oct);
Memo1.Lines.Add('NOT i: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
s1:= MyIntToHex(i);
Memo1.Lines.Add('Base is hexadecimal: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinNOT(s1,Hex);
Memo1.Lines.Add('NOT i: ' + s1);
end;
{ Added 24-th, late at night... }
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
s1:= MyIntToHex(i);
Memo1.Lines.Add('Base is hexadecimal: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinShiftLeft(s1,Hex);
Memo1.Lines.Add('Binary 1 bit shift left: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
s1:= MyIntToHex(i);
Memo1.Lines.Add('Base is hexadecimal: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinShiftRight(s1,Hex);
Memo1.Lines.Add('Binary 1 bit shift right: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
s1:= MyIntToHex(i);
Memo1.Lines.Add('Base is hexadecimal: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinShiftLeftByte(s1,Hex);
Memo1.Lines.Add('Binary 1 byte shift left: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
s1:= MyIntToHex(i);
Memo1.Lines.Add('Base is hexadecimal: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinShiftRightByte(s1,Hex);
Memo1.Lines.Add('Binary 1 byte right shift: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
s1:= IntToOct(i);
Memo1.Lines.Add('Base is octal: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinShiftLeft(s1,Oct);
Memo1.Lines.Add('Binary 1 bit shift left: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
s1:= IntToOct(i);
Memo1.Lines.Add('Base is octal: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinShiftRight(s1,Oct);
Memo1.Lines.Add('Binary 1 bit shift right: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
s1:= IntToOct(i);
Memo1.Lines.Add('Base is octal: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinShiftLeftByte(s1,Oct);
Memo1.Lines.Add('Binary 1 byte shift left: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(50000) + 1;
s1:= IntToOct(i);
Memo1.Lines.Add('Base is octal: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinShiftRightByte(s1,Oct);
Memo1.Lines.Add('Binary 1 byte right shift: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(5000) + 1;
s1:= IntToBin(i);
Memo1.Lines.Add('Base is binary: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinShiftLeft(s1,Bin);
Memo1.Lines.Add('Binary 1 bit shift left: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(5000) + 1;
s1:= IntToBin(i);
Memo1.Lines.Add('Base is binary: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinShiftRight(s1,Bin);
Memo1.Lines.Add('Binary 1 bit shift right: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(5000) + 1;
s1:= IntToBin(i);
Memo1.Lines.Add('Base is binary: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinShiftLeftByte(s1,Bin);
Memo1.Lines.Add('Binary 1 byte shift left: ' + s1);
end;
for m:= 1 to 5 do
begin
i:= random(5000) + 1;
s1:= IntToBin(i);
Memo1.Lines.Add('Base is binary: ');
Memo1.Lines.Add('i= ' + s1);
s1:= BinShiftRightByte(s1,Bin);
Memo1.Lines.Add('Binary 1 byte right shift: ' + s1);
end;
Memo1.Lines.Add('Terminat hora diem, terminat auctor opus.');
end;
{ To ensure a readability on everyones screen... }
procedure TForm1.Font1Click(Sender: TObject);
begin
if FontDialog1.Execute then
Memo1.Font:= FontDialog1.Font;
end;
procedure TForm1.Clear1Click(Sender: TObject);
begin
if MessageDlg('Clear this memo?', mtInformation,
[mbOk, mbCancel], 0) = id_OK then
Memo1.Lines.Clear;
end;
end.